﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.TagHelpers.Cache;
using Microsoft.VisualBasic;
using MyBlog.IService;
using MyBlog.Model;
using MyBlog.Service;
using MyBlog.WebApi.Dtos;
using MyBlog.WebApi.Utility;
using SqlSugar;

namespace MyBlog.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BlogNewsController : ControllerBase
    {
        private readonly IBlogNewsService _blogNewsService;

        public BlogNewsController(IBlogNewsService blogNewsService)
        {
            _blogNewsService = blogNewsService;
        }


        /// <summary>
        /// 获取博客
        /// </summary>
        /// <returns></returns>
        [HttpGet("BlogNews")]
        public async Task<ActionResult<ApiResult>> GetBlogNews()
        {
            var data = await _blogNewsService.QueryAsync();
            if (data == null)
            {
                return ApiResultHelper.Error("数据为空");
            }
            return ApiResultHelper.Success(data);
        }

        /// <summary>
        /// 添加文章
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="typeId"></param>
        /// <returns></returns>
        [HttpPost("Create")]
        public async Task<ActionResult<ApiResult>> Create(string title, string content, int typeId)
        {
            // 数据验证
            BlogNews blogNews = new BlogNews
            {
                BrowseCount = 0,
                Content = content,
                LikeCount = 0,
                Time = DateTime.Now,
                Title = title,
                TypeId = typeId,
                WriterId = 1
            };
            bool b = await _blogNewsService.CreateAsync(blogNews);
            if (!b)
            {
                return ApiResultHelper.Error("添加失败，服务器发生错误");
            }
            return ApiResultHelper.Success(blogNews);

        }

        /// <summary>
        /// 删除文章
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("Delete")]
        public async Task<ActionResult<ApiResult>> Delete(int id)
        {
            bool b = await _blogNewsService.DeleteAsync(id);
            if (!b)
                return ApiResultHelper.Error("删除失败");
            return ApiResultHelper.Success(b);
        }


        /// <summary>
        /// 编辑文章
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut("Edit")]
        public async Task<ActionResult<ApiResult>> Edit(int id, string title, string content, int typeId)
        {
            var blogNews = await _blogNewsService.FindAsync(id);
            if (blogNews == null)
                return ApiResultHelper.Error("文章不存在");
            blogNews.Title = title;
            blogNews.Content = content;
            blogNews.TypeId = typeId;
            bool b = await _blogNewsService.EditAsync(blogNews);
            if (!b)
                return ApiResultHelper.Error("修改失败");
            return ApiResultHelper.Success(blogNews);
        }

        /// <summary>
        /// 分页获取文章
        /// </summary>
        /// <param name="iMapper"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet("BlogNewsPage")]
        public async Task<ApiResult> BlogNewsPage([FromServices] IMapper iMapper, int page, int size)
        {
            RefAsync<int> total = 0;
            var blogNews = await _blogNewsService.QueryAsync(page, size, total);
            try
            {
                var blogNewsDTO = iMapper.Map<List<BlogNewsDto>>(blogNews);
                return ApiResultHelper.Success(blogNewsDTO, total);
            }
            catch (Exception ex)
            {
                return ApiResultHelper.Error("AutoMapper映射失败");
            }
        }

    }
}
